// pages/mine-shenqing/mine-shenqing.js
const myRequest = require("../../utils/require.js")
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    status:'',
    list: [],
    next: true,
    page: 1,
    search_key: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    app.CheckSession().then(res => {
      if (res == true) {
        app.GetUser().then(data => {
          this.apiRequset()
          this.setData({
            userInfo: data
          })
        })
      }
    })
  },
  Chongxin(e){
    let index=e.currentTarget.dataset.index
    myRequest.myRequest('DisSource/apply/' + this.data.list[index].workshop_id, { code:this.data.list[index].code}, 'POST').then(res => {
          wx.showToast({
            title: res.message,   
            success: res=> {
              this.setData({
                list: [],
                page: 1,
                next:true
              })
              this.apiRequset()
             },
            fail: function (res) { },
            complete: function (res) { },
          })
        
      })
  },
  Cancle(e){
    let index = e.currentTarget.dataset.index
    myRequest.myRequest('DisSource/cancel/' + this.data.list[index].code, {  }, 'POST').then(res => {
      if(res.code=='success'){
        wx.showToast({
          title: res.message,
          success: res => {
            this.setData({
              list: [],
              page: 1,
              next: true
            })
            this.apiRequset()
          },
          fail: function (res) { },
          complete: function (res) { },
        })
      }else{
        wx.showToast({
          title: res.message,

          success: function(res) {},
          fail: function(res) {},
          complete: function(res) {},
        })
      }

    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  Search(e) {
    console.log(e)
    this.setData({
      search_key: e.detail.value
    })
  },
  Sousuo(e) {
    this.setData({
      list: [],
      page: 1,
      next: true
    })

    this.apiRequset()
  },
  NavChange(e) {
    this.setData({
      status: e.currentTarget.dataset.cur,
      page: 1,
      list: [],
      next: true
    });
    this.apiRequset();
  },
  apiRequset() {
    if (this.data.next == null) {
      return
    }
    myRequest.myRequest('user/applyList', { page: this.data.page, search_key: this.data.search_key, status: this.data.status }, 'GET').then(list => {
      console.log(list)
      let page = this.data.page
      let data = this.data.list
      data = data.concat(list.data)
      page++
      this.setData({
        list: data,
        page: page,
        next: list.links.next
      })
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.apiRequset()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})